﻿using DidaCode.Workflow.Logs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DidaCode.Workflow.Models
{
    public class NodeResult<TResult>
    {
        /// <summary>
        ///节点执行结果
        ///
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 执行返回结果
        /// </summary>
        public TResult? Value { get; set; }

        /// <summary>
        /// 节点执行结果日志
        /// </summary>
        public Dictionary<string, TypedResult> AdditionalData { get; set; } = new Dictionary<string, TypedResult>();

        #region ResultNode



        /// <summary>
        /// 节点执行成功
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> SuccessNode(TResult t) => GetNodeResult(t);


        /// <summary>
        /// 节点执行成功
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> SuccessNode() => DefaultSuccessNode;

        /// <summary>
        /// 节点执行成功
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> DefaultSuccessNode => GetNodeResult();

        /// <summary>
        ///节点执行失败，继续执行
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> FailNode(TResult t) => GetNodeResult(isSuccess: false);


        /// <summary>
        /// 节点执行失败，继续执行
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> FailNode() => DefaultFailNode;

        /// <summary>
        /// 节点执行失败，继续执行
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> DefaultFailNode => GetNodeResult(isSuccess: false);


        /// <summary>
        /// 节点执行失败且终止执行
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> EndFailNode(TResult t) => GetNodeResult(t, false, false);


        /// <summary>
        /// 节点执行失败且终止执行
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> EndFailNode() => DefaultEndFailNode;


        /// <summary>
        /// 节点执行失败且终止执行
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static NodeResult<TResult> DefaultEndFailNode => GetNodeResult(isSuccess: false, continueOnFailure: false);



        /// <summary>
        ///
        /// </summary>
        /// <param name="t"></param>
        /// <param name="isSuccess"></param>
        /// <param name="continueOnFailure"></param>
        /// <returns></returns>
        private static NodeResult<TResult> GetNodeResult(TResult t = default, bool isSuccess = true, bool continueOnFailure = true)
        {
            return new NodeResult<TResult>
            {
                IsSuccess = isSuccess,
                Value = t,
            };
        }

        #endregion


    }
}
